﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PhysioAssist2
{
    public partial class AdminAvisosEditar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    Session["id"] = Request["id"].ToString();
                    CarregaAvisos(int.Parse(Session["id"].ToString()));
                }
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {

            //Salva Anúncio
            dsTableAdapters.anuncioTableAdapter ta = new dsTableAdapters.anuncioTableAdapter();

            if (Request["id"] == null)
            {
                ta.Insert(DateTime.Now, txtMensagem.Text,10);//HARDCODE
            }
            else
            {
                ta.Update(DateTime.Parse(Session["data"].ToString()), txtMensagem.Text, 10, int.Parse(Session["id"].ToString()));//HARDCODE
            }

            Response.Redirect("AdminAvisos.aspx");

        }

        private void CarregaAvisos(int id)
        {

            WS ws = new WS();
            Anuncio aviso = ws.GetAnuncio(id);

            txtMensagem.Text = aviso.mensagem;
            Session["data"] = aviso.data;
        }
    }
}